﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class RolesService : IRolesService
    {
        private IRolesDal _roles;
        private IMapper _mapper;
        public RolesService(IRolesDal roles, IMapper mapper)
        {
            _roles = roles;
            _mapper = mapper;
        }
        public async Task<int> AddRolesAsync(string title)
        {
            return await _roles.AddAsync(new Role 
            {
                Title = title
            });
        }

        public async Task<int> DeleteRolesAsync(int id)
        {
            var data = await _roles.QueryAsync(id);
            return data == null ? -1 : await _roles.DeleteAsync(data);
        }

        public async Task<int> EditRolesAsync(int id, string title)
        {
            var data = await _roles.QueryAsync(id);
            if (data == null)
                return -1;
            data.Title = title;
            return await _roles.EditAsync(data);
        }

        public async Task<List<RolesDto>> QueryAllRolesAsync()
        {
            var data = await _roles.QueryAsync();
            return _mapper.Map<List<Role>,List<RolesDto>>(data);
        }

        public async Task<RolesDto> QueryByIdRolesAsync(int id)
        {
            var data = await _roles.QueryAsync(id);
            return _mapper.Map<Role,RolesDto>(data);
        }

        public async Task<List<RolesDto>> QueryByTitleRolesAsync(string title)
        {
            var data = await _roles.QueryAsync(r => r.Title.Contains(title));
            return _mapper.Map<List<Role>,List<RolesDto>>(data);
        }

        public async Task<string> QueryRoleTitleAsync(int id)
        {
            var data = await _roles.QueryAsync(id);
            return data == null ? "" : data.Title;
        }
    }
}
